package com.www.applyword.ui.my;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;

import com.allen.library.SuperButton;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/29 0029 17:09
 * version: 1.0
 */
public class MyRealNameActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.sb_code)
    SuperButton sbCode;
    @BindView(R.id.et_real_code)
    EditText etRealCode;
    private VerificationCodeCountTimer verificationCodeCountTimer;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.real_name_activity);
        setTitle("实名认证");
        setRringTitle("提交");
        tv_right.setOnClickListener(v -> realNameAuthentication());
    }

    private void realNameAuthentication() {
        if (TextUtils.isEmpty(etName.getText().toString().trim())){
            Utils.showToastSafe("请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())){
            Utils.showToastSafe("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(etCode.getText().toString().trim())){
            Utils.showToastSafe("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(etRealCode.getText().toString().trim())){
            Utils.showToastSafe("请输入身份证号");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("RealName",etName.getText().toString().trim());
        params.put("Mobile",etPhone.getText().toString().trim());
        params.put("VerificationCode",etCode.getText().toString().trim());
        params.put("IDCard",etRealCode.getText().toString().trim());
        HttpClient.post(MyRealNameActivity.this, Constants.REALNAMEAUTHENTICATION, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.REAL_NAME);
              //  EventBus.getDefault().post(messageEvent);

                Utils.showToastSafe("提交成功");
                finish();
            }
        });
    }

    @Override
    protected void loadData() {
        verificationCodeCountTimer = new VerificationCodeCountTimer(61100, 1000);
    }



    @OnClick(R.id.sb_code)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())){
            Utils.showToastSafe("请输入手机号");
            return;
        }
        verificationCodeCountTimer.start();
        Map<String, Object> params = new HashMap<>();
        params.put("phone",etPhone.getText().toString());
        HttpClient.get(MyRealNameActivity.this, Constants.GETSMSCODE, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
              //  etCode.setText(result);
            }
        });
    }

    class VerificationCodeCountTimer extends CountDownTimer {

        private VerificationCodeCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            int time = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
          //  sbCode.setTextColor(getResources().getColor(R.color.color_yellow2));
            sbCode.setText(time + "s");
        }

        @Override
        public void onFinish() {
            sbCode.setText("获取验证码");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (verificationCodeCountTimer != null) {
            verificationCodeCountTimer.cancel();
        }
    }
}
